package net.arogarth.aws.android.lib;

import java.util.ArrayList;
import java.util.List;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import net.arogarth.aws.android.lib.models.RdsDbInstance;

public class AmazonRds extends AmazonAWS {
	public AmazonRds(String awsAccessKey, String awsSecretKey) {
		super();

		this.service = "rds";
		
		if(!AwsSettings.getAwsRegion().equals(""))
			this.service += "." + AwsSettings.getAwsRegion();
		
		this.version = "2011-04-01";
	}

	public List<RdsDbInstance> descriptDbInstances() throws AwsRequestException {
		List<RdsDbInstance> RdsDbInstances = new ArrayList<RdsDbInstance>();

		params.put("Action", "DescribeDBInstances");
		Element root = this.execute();

		XPath xpath = XPathFactory.newInstance().newXPath();

		try {
			NodeList items = (NodeList) xpath.evaluate("//DBInstances/*", root,
					XPathConstants.NODESET);

			for (int i = 0; i < items.getLength(); i++) {
				RdsDbInstance RdsDbInstance = new RdsDbInstance();
				Node item = items.item(i);
				NodeList properties = item.getChildNodes();

				for (int j = 0; j < properties.getLength(); j++) {
					Node property = properties.item(j);
					String name = property.getNodeName();
					if (property.getFirstChild() == null)
						continue;
					String value = property.getFirstChild().getNodeValue();

					if (name.equalsIgnoreCase("ReadReplicaDBInstanceIdentifiers")) {
						RdsDbInstance
								.setReadReplicaDBInstanceIdentifiers(value);
					} else if (name.equalsIgnoreCase("ReadReplicaSourceDBInstanceIdentifier")) {
						RdsDbInstance
						.setReadReplicaSourceDBInstanceIdentifier(value);
					} else if (name.equalsIgnoreCase("LatestRestorableTime")) {
						RdsDbInstance.setLatestRestorableTime(value);
					} else if (name.equalsIgnoreCase("Engine")) {
						RdsDbInstance.setEngine(value);
					} else if (name.equalsIgnoreCase("PendingModifiedValues")) {
						RdsDbInstance.setPendingModifiedValues(value);
					} else if (name.equalsIgnoreCase("BackupRetentionPeriod")) {
						RdsDbInstance.setBackupRetentionPeriod(value);
					} else if (name.equalsIgnoreCase("MultiAZ")) {
						RdsDbInstance
								.setMultiAZ(value.equalsIgnoreCase("true"));
					} else if (name.equalsIgnoreCase("LicenseModel")) {
						RdsDbInstance.setLicenseModel(value);
					} else if (name.equalsIgnoreCase("DBInstanceStatus")) {
						RdsDbInstance.setDbInstanceStatus(value);
					} else if (name.equalsIgnoreCase("EngineVersion")) {
						RdsDbInstance.setEngineVersion(value);
					} else if (name.equalsIgnoreCase("Endpoint")) {
						Node endpoint = property.getFirstChild();
						do {
							if (endpoint.getNodeName().equalsIgnoreCase("port")) {
								RdsDbInstance.setEndpointPort(Integer
										.getInteger(endpoint.getFirstChild()
												.getNodeValue()));
							} else if (endpoint.getNodeName().equalsIgnoreCase(
									"Address")) {
								RdsDbInstance.setEndpointUrl(endpoint
										.getFirstChild().getNodeValue());

							}
						} while ((endpoint = endpoint.getNextSibling()) != null);

					} else if (name.equalsIgnoreCase("DBInstanceIdentifier")) {
						RdsDbInstance.setDbInstanceIdentifier(value);
					} else if (name.equalsIgnoreCase("DBParameterGroups")) {

					} else if (name.equalsIgnoreCase("DBSecurityGroups")) {

					} else if (name.equalsIgnoreCase("PreferredBackupWindow")) {
						RdsDbInstance.setPreferredBackupWindow(value);
					} else if (name.equalsIgnoreCase("AutoMinorVersionUpgrade")) {
						RdsDbInstance.setAutoMinorVersionUpgrade(value
								.equalsIgnoreCase("true"));
					} else if (name
							.equalsIgnoreCase("PreferredMaintenanceWindow")) {
						RdsDbInstance.setPreferredMaintenanceWindow(value);
					} else if (name.equalsIgnoreCase("AvailabilityZone")) {
						RdsDbInstance.setAvailabilityZone(value);
					} else if (name.equalsIgnoreCase("InstanceCreateTime")) {
						RdsDbInstance.setInstanceCreateTime(value);
					} else if (name.equalsIgnoreCase("AllocatedStorage")) {
						RdsDbInstance.setAllocatedStorage(value);
					} else if (name.equalsIgnoreCase("DBInstanceClass")) {
						RdsDbInstance.setDbInstanceClass(value);
					} else if (name.equalsIgnoreCase("MasterUsername")) {
						RdsDbInstance.setMasterUsername(value);
					}
				}

				RdsDbInstances.add(RdsDbInstance);
			}
		} catch (XPathExpressionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return RdsDbInstances;
	}
}
